const app = getApp();
const common = require('../../../utils/common.js');
const api = require('../../../config/api.js');

Page({
  data: {
    region: ['山东省', '青岛市', '市北区'],
    name: '',
    mobile: '',
    address: '',
    isDefault: 1,
  },
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  bindinputMobile(event) {
    this.setData({
      mobile: event.detail.value
    });
  },
  bindinputName(event) {
    this.setData({
      name: event.detail.value
    });
  },
  bindinputAddress (event){
    this.setData({
      address: event.detail.value
    });
  },
  bindIsDefault(){
    let isDefault = this.data.isDefault;
    if (isDefault == 1) {
      isDefault = 0;
    } else {
      isDefault = 1;
    }
    this.setData({
      isDefault: isDefault
    });
  },
  saveAddress(){
    let that = this;
    var data = {
      name: that.data.name,
      mobile: that.data.mobile,
      address: that.data.address,
      isDefault: that.data.isDefault,
      region: that.data.region
    }
		common.request(api.AddressSave, data, 'POST').then(function (data) {
      if (data.type === 'success') {
				common.showSuccessToast(data.content);
        setTimeout(function () {
          wx.navigateBack({ changed: true });
        }, 1500)
      } else {
				common.showErrorToast(data.content);
      }
    });
  }
})